
package com.showcai.controller.user;

import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.AdminUser;
import com.showcai.module.Role;
import com.showcai.module.User;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.AdminUserService;
import com.showcai.service.RoleService;
import com.showcai.service.UserService;
import com.showcai.util.MD5Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/user")
@Controller
public class UserController extends BaseController {

    @Autowired
    AdminUserService adminUserService;

    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(User user) {
        logger.info("userId===>>" + user);

        ResultObj resultObj = new ResultObj();
        try {
//            resultObj.setData(adminUserService.login(user));user
//            (userService.getUserById(userId));
            resultObj.setMsg("操作成功！");

        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    @RequestMapping(value = "/getUserList")
    public ModelAndView getUserList(Integer page, Integer pageSize) {
        logger.info("getUserList===>>");
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        ModelAndView mov = new ModelAndView();
        try {
            mov.addObject("pageInfo", adminUserService.getUserList(page, pageSize));
            mov.setViewName("user/getAdminUserList");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mov;
    }

    @RequestMapping(value = "/UserAdd")
    @ResponseBody
    public ModelAndView UserAdd(Long tid) {
        ModelAndView m = new ModelAndView("/user/UserAdd");
        List<Role> list = roleService.getRoleList();
        Map<String, String> map = new HashMap<String, String>();
        map.put("tid", String.valueOf(tid));
        AdminUser adminUser = adminUserService.selectAdminById(map);
        m.addObject("adminUser", adminUser);
        m.addObject("roleList", list);
        m.addObject("tid", tid);
        return m;
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public String addUser(AdminUser adminuser) {
        ResultObj resultObj = new ResultObj();
        try {
            logger.info("adminUsergetTid" + adminuser.getTid());
            logger.info("adminUsergetStatus" + adminuser.getStatus());
            adminUserService.modifyAdminUser(adminuser);

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg("操作失败！" + e.getMessage());
        }
        return resultObj.toJson();
    }


    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    @ResponseBody
    public String getAdvertList(HttpServletRequest request) {
        System.out.println("===============================loginOut============================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            request.getSession().invalidate();
            resultObj.setCode(ReturnCode.SUCCESS.getCode());
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj.toJson();

    }

    @RequestMapping(value = "/selectUserName", method = RequestMethod.POST)
    @ResponseBody
    public String selectUserName(String userName) {
        AdminUser adminUser = adminUserService.selectAdminUserName(userName);
        ResultObj resultObj = new ResultObj();
        resultObj.setData(adminUser);
        return resultObj.toJson();
    }

    @RequestMapping(value = "/stopAdminUser", method = RequestMethod.POST)
    @ResponseBody
    public String stopAdminUser(Long tid) {
        ResultObj resultObj = new ResultObj();
        adminUserService.stopAdminUser(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }

    @RequestMapping(value = "/operateAdminUser", method = RequestMethod.POST)
    @ResponseBody
    public String operateAdminUser(Long tid) {
        ResultObj resultObj = new ResultObj();
        adminUserService.operateAdminUser(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }

    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    @ResponseBody
    public String resetPassword(Long tid) {
        ResultObj resultObj = new ResultObj();
        adminUserService.resetPassword(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
    @RequestMapping(value = "/updatePassWordPage")
    public ModelAndView updatePassWordPage() {
        ModelAndView mov = new ModelAndView("user/updatePassWord");
        return mov;
    }
    @RequestMapping(value = "/updatePassWord", method = RequestMethod.POST)
    @ResponseBody
    public String updatePassWord(String passWord,String newPassWord) {
        ResultObj resultObj = new ResultObj();
        try {
            AdminUser adminUser = (AdminUser)super.session.getAttribute(Constant.SESSION_KEY_ADMIN_USER_LOGIN);
            if(adminUser==null){
                throw new MyRunTimeExcption("用户不存在！");
            }
            if(!MD5Tools.MD5(passWord).equals(adminUser.getPassWord())){
                throw new MyRunTimeExcption("旧密码错误请重新输入！");
            }
            adminUser.setPassWord(MD5Tools.MD5(newPassWord));
            adminUserService.modifyAdminUser(adminUser);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        }
        catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj.toJson();
    }

}
